(function ($) {
    $.fn.yqTextUiMask = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTextUiMask.methods[opts](this, param);
        }

        $this = this;
        var opt;
        if ($this.data("yqtype") == "yqTextUiMask") {
            var $uniqueID = $this.attr("ID");
            destroy($this);
            $this = $("#" + $uniqueID);
        } else {
            var $uniqueID = $this.attr("ID");
            if ($uniqueID === undefined) {
                $uniqueID = $.getUniqueID();
                $this.attr("ID", $uniqueID);
            }
        }
        opt = $.extend({}, $.fn.yqTextUiMask.defaults, opts);

        if (opt.width === undefined && $this.width() > 0) {
            opt.width = $this.width();
        }

        //变量信息
        $this.data("textuimask_opt", opt);
        $this.data("yqtype", "yqTextUiMask");

        init_text($this);

        return $this;
    };

    $.fn.yqTextUiMask.defaults = {
        ajaxUrl: "/_db.html",
        ui_type: 2,
        value: {},
        isEdit: true,
        treeEdit: false,
        editModeW: 500, //模态编辑器宽度 格式为 500（数字）
        editModeH: 400, //模态编辑器高度 格式为 400（数字）
        beforeAdd: function (rows, row, callback) {
            callback(row);
        },
        beforeChange: function (rows, callback) {
            callback(true, rows);
        },
        afterChange: function (rows) { //修改后

        },
        beforeEditShow: function (run_opt) {
            return run_opt;
        }
    };

    $.fn.yqTextUiMask.methods = {
        options: function (jq) {
            return options(jq);
        },
        destroy: function (jq) {
            destroy(jq);
        },
        resize: function (jq, width) {
            resize(jq, width);
        },
        clear: function (jq) {
            setRows(jq, {});
        },
        setValue: function (jq, value) {
            setValue(jq, value);
        },
        getValue: function (jq) {
            return getValue(jq);
        },
        isEdit: function (jq, tf) {
            isEdit(jq, tf);
        }
    };

    function options($object) {
        return $object.data("textuimask_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text($object) {
        var opt = options($object);

        var text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'icon2-edit',
                handler: function (e) {
                    var pp = $object.textbox("textbox");
                    prevent_e = $.getPreventDefault(pp[0]);
                    prevent_e.id = $object.attr("id");
                    prevent_e.options = opt;

                    var mdiinfo = $.yqGetWindParams();

                    var run_opt = {
                        mdiinfo: mdiinfo,
                        prevent_e: prevent_e,
                        width: opt.editModeW,
                        height: opt.editModeH,
                        ui_type: opt.ui_type,
                        value: opt.value,
                        isEdit: opt.isEdit,
                        treeEdit: opt.treeEdit,
                        callback: function (rows) {
                            //嵌入到datagrid中，兼容处理（需要重新创建对象）
                            if (typeof opt.p_target != "undefined") {
                                var ed = $.yqTextEditor(opt.p_target, opt.p_field);
                                if (!(ed === null)) {
                                    $(ed.target).yqTextUiMask('setValue', rows);
                                    opt.afterChange.call($(ed.target), rows);
                                }
                            }
                            else {
                                setValue($object, rows);
                                opt.afterChange.call($(ed.target), rows);
                            }
                        }
                    };
                    
                    run_opt = opt.beforeEditShow.call($object,run_opt);

                    if (window.top == window.self) {
                        $.yqUiMaskPopEdit(run_opt);
                    }
                    else {
                        window.top.$.yqUiMaskPopEdit(run_opt);
                    }
                }
            }, {
                iconCls: 'icon-clear',
                handler: function (e) {
                    setValue($object, {
                        titles: [],
                        model: []
                    });
                }
            }]
        };

        $object.textbox(text_opt);

        setValue($object, opt.value);
        isEdit($object, opt.isEdit);
    }

    function setValue($object, value) {
        var opt = options($object);
        if (value === undefined || value === null || value == "") {
            value = {};
        } else if (typeof value === "string") {
            value = JSON.parse(value);
        }
        opt.value = value;
        setText($object);
    }

    function getValue($object) {
        var opt = options($object);
        return opt.value;
    }

    function setText($object) {
        var opt = options($object);
        if (Object.getOwnPropertyNames(opt.value).length > 0) {
            $object.textbox("setText", JSON.stringify(opt.value));
        }
        else {
            $object.textbox("setText", "");
        }
    }

    function isEdit($object, tf) {
        var opt = options($object);
        var icon0 = $object.textbox("getIcon", 0);
        var icon1 = $object.textbox("getIcon", 1);
        opt.isEdit = tf;
        if (opt.isEdit) {
            icon0.removeClass('icon-more');
            icon0.addClass('icon-edit');
            icon1.show();
        } else {
            icon0.removeClass('icon-edit');
            icon0.addClass('icon-more');
            icon1.hide();
        }
    }
})(jQuery);
